const conn = require('../../libs/DB/Conn');
const utils = require('../../libs/Utils');
const operationRecord = require("../sys/operationRecordAction");
module.exports = async function (params) {
    let checkUserSql = `SELECT      
                          count(*) AS count
                        FROM
                          user_file uf                         
                        WHERE login_name = ? AND delete_sign = 1;
                        SELECT
                           count(*) AS count
                        FROM
                           user_group 
                        WHERE domain_id = ?  AND group_id = ? AND delete_sign = 1`;
    let queryResult = await conn(checkUserSql, [params.loginName,params.userDomainId,params.groupId]);
    if (queryResult[0][0].count > 0) {
        return {success: false, message: '此登录名已存在，请重新输入登录名'};
    }else if(queryResult[1][0].count === 0) {
        return {success: true, message: '当前管理的区域下没有此用户组'};
    }else {
        let insertObj = {
            login_name: params.loginName,
            password: utils.encrypt("123456"),//用户默认密码为123456
            user_name: params.userName,
            group_id: params.groupId,
            mobile: params.mobile,
            domain_id:params.userDomainId
        };
        let insertSql = `INSERT INTO user_file SET ?`;
        await conn(insertSql, [insertObj]);
        await operationRecord(params);
        return {success: true, message: '添加成功'}
    }
};